UI = function() {
	var results;
	var showlog = false;
	var showresults = false;
	
	/** Templates */
	var nameTemplateName = '_nameTemplateName';
	var winsTemplateName = '_winsTemplateName';
	var lossesTemplateName = '_lossesTemplateName';
	var retiredNumbersTemplateName = '_retiredNumbersTemplateName';
	var nicknameTemplateName = '_nicknameTemplateName';
	var colorTemplateName = '_colorTemplateName';
	var ownerTemplateName = '_ownerTemplateName';
	var cityTemplateName = '_cityTemplateName';
	var stateTemplateName = '_stateTemplateName';
	var stadiumTemplateName = '_stadiumTemplateName';
	var foundedTemplateName = '_foundedTemplateName';
	var joinedTemplateName = '_joinedTemplateName';
	var divisionTemplateName = '_divisionTemplateName';
	var leagueTemplateName = '_leagueTemplateName';
	var conferenceTemplateName = '_conferenceTemplateName';
	var championshipTemplateName = '_championshipTemplateName';
	var managerTemplateName = '_managerTemplateName';
	
	/** TODO: Think about moving attributes to metadata object */
	var build_simple_select_query = function() {
		/** attributes */
		var opts = '';
		$.each(METADATA, function(index, value) {
			opts += '<option value="' + value.key + '">' + value.value + '</option>';
		});
		$('#simpleQueryAttribute').html(opts);
		
		/** Operations */
		opts = '';
		$.each(SimpleOperations, function(index, value) {
			opts += '<option value="' + value.key + '">' + value.value + '</option>';
		});
		$('#simpleQueryOperation').html(opts);		
	};
	
	var build_aggregate_select_query = function() {
		/** attributes - should be moved to common fn w/ build_simple_select_query*/
		var opts = '';
		$.each(METADATA, function(index, value) {
			opts += '<option value="' + value.key + '">' + value.value + '</option>';
		});
		$('#aggregateQueryAttribute').html(opts);
		
	}
	
	/** Public **/
	return {
		/** 
			Constructor 
		*/
		init : function(conf) {
			var conf = conf || {};
			if (conf.showlog) {
				showlog = true;
				$('#log-container').show()
			} else {
				$('#log-container').hide();
			}
			if (conf.showresults) {
				showresults = true;
				$('#nlpResults').show()
			} else {
				$('#nlpResults').hide();
			}
			results = $('#results');
			
			/** precompile templates **/
			$.template(nameTemplateName, $('#result_nameTemplate'));
			$.template(winsTemplateName, $('#result_winsTemplate'));
			$.template(lossesTemplateName, $('#result_lossesTemplate'));
			$.template(retiredNumbersTemplateName, $('#result_retiredNumbersTemplate'));
			$.template(nicknameTemplateName, $('#result_nicknameTemplate'));
			$.template(colorTemplateName, $('#result_colorTemplate'));
			$.template(ownerTemplateName, $('#result_ownerTemplate'));
			$.template(cityTemplateName, $('#result_cityTemplate'));
			$.template(stateTemplateName, $('#result_stateTemplate'));
			$.template(stadiumTemplateName, $('#result_stadiumTemplate'));
			$.template(foundedTemplateName, $('#result_foundedTemplate'));
			$.template(joinedTemplateName, $('#result_joinedTemplate'));
			$.template(divisionTemplateName, $('#result_divisionTemplate'));
			$.template(leagueTemplateName, $('#result_leagueTemplate'));
			$.template(conferenceTemplateName, $('#result_conferenceTemplate'));
			$.template(championshipTemplateName, $('#result_championshipTemplate'));
			$.template(managerTemplateName, $('#result_managerTemplate'));
			
			/** build dynamic areas */
			build_simple_select_query();
			build_aggregate_select_query();
			
			/** hide other areas */
			$('#frequency-results').hide();
		},
				
		/** Display Teams in result table*/
		display_team_table : function(teams_list) {
			results.dataTable({
				"bDestroy":true,
				"aaData": teams_list,
				"aoColumns": [
					{ sWidth: '10%', fnRender: function (oObj) { return $.tmpl(nameTemplateName, oObj.aData).text(); }},
					{ sWidth: '5%', fnRender: function (oObj) { return $.tmpl(winsTemplateName, oObj.aData).text(); }},
					{ sWidth: '5%', fnRender: function (oObj) { return $.tmpl(lossesTemplateName, oObj.aData).text(); }},
					{ sWidth: '5%', fnRender: function (oObj) { return $.tmpl(retiredNumbersTemplateName, oObj.aData).text(); }},
					{ sWidth: '13%', fnRender: function (oObj) { return $.tmpl(nicknameTemplateName, oObj.aData).text(); }},
					{ sWidth: '11%', fnRender: function (oObj) { return $.tmpl(colorTemplateName, oObj.aData).text(); }},
					{ sWidth: '10%', fnRender: function (oObj) { return $.tmpl(ownerTemplateName, oObj.aData).text(); }},
					{ sWidth: '5%', fnRender: function (oObj) { return $.tmpl(cityTemplateName, oObj.aData).text(); }},
					{ sWidth: '5%', fnRender: function (oObj) { return $.tmpl(stateTemplateName, oObj.aData).text(); }},
					{ sWidth: '5%', fnRender: function (oObj) { return $.tmpl(stadiumTemplateName, oObj.aData).text(); }},
					{ sWidth: '5%', fnRender: function (oObj) { return $.tmpl(foundedTemplateName, oObj.aData).text(); }},
					{ sWidth: '5%', fnRender: function (oObj) { return $.tmpl(joinedTemplateName, oObj.aData).text(); }},
					{ sWidth: '5%', fnRender: function (oObj) { return $.tmpl(divisionTemplateName, oObj.aData).text(); }},
					{ sWidth: '5%', fnRender: function (oObj) { return $.tmpl(leagueTemplateName, oObj.aData).text(); }},
					{ sWidth: '5%', fnRender: function (oObj) { return $.tmpl(conferenceTemplateName, oObj.aData).text(); }},
					{ sWidth: '7%', fnRender: function (oObj) { return $.tmpl(championshipTemplateName, oObj.aData).text(); }},
					{ sWidth: '7%', fnRender: function (oObj) { return $.tmpl(managerTemplateName, oObj.aData).text(); }}
				]});
				$('#frequency-results').hide();
				$('#results-table').show();	
		},
		
		/** Display frequency table.  Tuples is a [{key:k,value:v},{key:k,value:v},...] structure */
		display_frequency_table : function(tuples) {
			$('#frequency-table').children().remove();
			var convert = [];
			$.each(tuples, function(idx, elem) { convert.push({'key':elem[0],'value':elem[1]} )});
			$('#frequencyTemplate').tmpl(convert).appendTo('#frequency-table');
			$('#results-table').hide();
			$('#frequency-results').show();
		},
		
		/** Display nlp results*/
		 display_nlp_results : function( query, results ) {
			var message;
			if(results.length === 0) {
				message = "There are no " + query.display_attr + " with " 
				+ query.attr + " " + query.operation + " " + query.value + ".";
			} else {
				var toBe = results.length === 1 ? " is " : " are ";
				var team_names = [];
				$.each(results, function(idx,team){ team_names.push(team[query.display_attr]); });
				team_names = team_names.sort(); /** todo: handle numerics? */
				message = "The " + query.display_attr + " of " + query.subject + " with " 
				+ query.attr + " " + query.operation + " " + query.value 
				+ toBe + team_names.join(', ') + ".";
			}
			$( "#nlpResults" ).text("");
			$( "<div/>" ).text( message ).prependTo( "#nlpResults" );
			$( "#nlpResults" ).scrollTop( 0 );
			
			if (!$('#nlpResults').is(":visible")) {
				UI.toggleResultsVisibility();
			}
		},
		
		/** General Log */
		 log : function( message ) {
			$( "<div/>" ).text( message ).prependTo( "#log" );
			$( "#log" ).scrollTop( 0 );
		},
		
		toggleLogVisibility: function() {
			showlog = !showlog;
			if(showlog)
				$( "#log-container" ).show();
			else
				$( "#log-container" ).hide();
		},
		
		toggleResultsVisibility: function() {
			showresults = !showresults;
			if(showresults)
				$( "#nlpResults" ).show();
			else
				$( "#nlpResults" ).hide();
		}
	}
}();
